package com.xywy.find.paser;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.xywy.R;
import com.xywy.common.constants.ResultCode;
import com.xywy.common.net.AsyncHttpClient;
import com.xywy.common.net.OnResultListenerAdapter;
import com.xywy.common.net.Parser;
import com.xywy.common.util.MLog;
import com.xywy.find.bean.JkzhArticle;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by xiaogd on 8/4/14.
 * 健康知乎 -内容详情页 json解析类
 *  (1) 获取 文章
 *  (2) 赞
 *  (3) 收藏
 *
 *
 */
public class JkzhArticlePaser {
    public static final int ARTICLE = 1;   //(1) 获取 文章
    public static final int GOOD = 2;      //(2) 赞
    public static final int COLLECT = 3;   //(3) 收藏
    public static final int FIND = 4;

    private static final String  TAG = "JkzhArticlePaser";
    private Context  mContext;
    private Handler  mHandler;
    
    private int what;  //handler 的what 值

    public int getWhat() {
		return what;
	}
	public void setWhat(int what) {
		this.what = what;
	}
	public JkzhArticlePaser(Context context, Handler handler){
          this.mContext = context;
          this.mHandler  = handler;
    }

    //封装Parser参数
    private Parser paramsParser;
    //异步加载
    private AsyncHttpClient asyncHttpClient;

    //处理网络请求
    public void sendMsg(String  action,HashMap<String, Object> inParams,String  request) {
        paramsParser = new Parser();
        //请求路径
        paramsParser.action = action;
        //请求方式
        paramsParser.request = request;
        //参数
        paramsParser.map = inParams;
        //传递上下文
        paramsParser.context = this.mContext;

        asyncHttpClient = AsyncHttpClient.getInstance();
        asyncHttpClient.exeute(paramsParser,onResultListenerAdapter);
    }

    OnResultListenerAdapter onResultListenerAdapter =new OnResultListenerAdapter(){
        @Override
        public <T extends Parser> void onSuccess(T resultParser, int resultCode) {
            super.onSuccess(resultParser, resultCode);
            Message message = Message.obtain();
            message.arg1 = resultCode;
            Object  data=null;
            switch (what) {
			case ARTICLE:
				  //1. 数据详情
	            if(paramsParser.action.equals(mContext.getResources().getString(R.string.find_jkzh_article))){
	                String  back="";
	                if(resultCode== ResultCode.SUCCESS){
	                    try{
	                        JSONObject json =new JSONObject(resultParser.back);
	                        back = json.optString("data");
	                    }catch(JSONException  e){
	                        e.printStackTrace();
	                    }
	                 }
	                 data= getJkzhArticle(back);
	                 message.obj =data;
	            }
				break;
			case COLLECT:
				break;
			case FIND:
				  message.arg2 = what;
				  Log.e("find-------", resultParser.back+"");
					 message.obj =resultParser.back;
			        mHandler.sendMessage(message);
					return;
			case GOOD:
				
				break;
			default:
				break;
			}
            message.arg2 = what;
            mHandler.sendMessage(message);
        }
    };
    /**
     * 1. 数据详情: 封装返回的json
     */
    private JkzhArticle getJkzhArticle(String  jsonStr) {

        JkzhArticle article = new JkzhArticle();

        try {
            JSONObject json = new JSONObject(jsonStr);
            article.setArtId(json.optString("art_id"));
            article.setArtImg(json.optString("art_img"));
            article.setNameD(json.optString("name_d"));
            article.setDocImg(json.optString("doc_img"));
            article.setType(json.optString("type"));
            article.setTitle(json.optString("title"));
            article.setPubtime(json.optLong("pubtime"));
            article.setContent(json.optString("content"));
            article.setDetail(json.optString("detail"));
            article.setUrl(json.optString("url"));

            MLog.i(TAG,"JkzhArticle JSON====="+article.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return    article;
    }
}
